<template>
  <div class="person-container">
    <div class="person_info">
      <div class="left">
        <img :src="userInfo.avatarUrl" alt="" />
      </div>
      <div class="right">
        <div class="user">
          <div class="userInfo">
            <div class="username">{{ userInfo.nickname }}</div>
            <div class="grade">
              <div class="grademain">Lv.{{ userInfo.level }}</div>
            </div>
          </div>
          <div class="logout" @click="logout">退出登录</div>
        </div>
        <div class="button">
          <div class="buttonItem">
            <p>{{ userInfo.eventCount }}</p>
            <p class="item">动态</p>
          </div>
          <div class="buttonItem" @click="goToMyLove">
            <p>{{ userInfo.newFollows }}</p>
            <p class="item">关注</p>
          </div>
          <div class="buttonItem" @click="goToMyFans">
            <p>{{ userInfo.followeds }}</p>
            <p class="item">粉丝</p>
          </div>
        </div>
        <div class="userId">用户ID：{{ userInfo.userId }}</div>
        <div class="region">所在地区: 四川省-绵阳市</div>
      </div>
    </div>
    <div class="person-index">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
// 将localStorage中的userId取出来 页面刷新会导致vuex重新加载数据所有直接从localStorage中取数据
let userId = JSON.parse(localStorage.getItem("userInfo")).userId;
export default {
  name: "Personal",
  data() {
    return {
      // 用户详情
      userInfo: {},
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      console.log(this.$store.state.userId);
      const { data: res } = await this.$http("/user/detail", {
        params: { uid: userId },
      });
      console.log(res);
      res.profile["level"] = res.level;
      this.userInfo = res.profile;
    },
    // 退出登录
    async logout() {
      const { data: res } = await this.$http("/logout");
      console.log(res);
      if (res.code != 200) {
        this.$message.error("退出登录失败，请稍后再试");
        return;
      }
      window.localStorage.removeItem("userInfo");
      this.$store.commit("updataLoginState", false);
      this.$message.success("退出成功!");
      this.$router.push("/discover");
      setTimeout(() => {
        this.$router.go(0);
      }, 1000);
    },
    //点击关注按钮事件
    goToMyLove() {
      this.$router.push("/mylove");
    },
    // 点击粉丝按钮
    goToMyFans() {
      this.$router.push("/myfans");
    },
  },
};
</script>

<style lang="less" scoped>
.person-container {
  width: 90%;
  margin: 20px auto;
  .person_info {
    display: flex;

    .left {
      img {
        width: 200px;
        height: 200px;
        margin-right: 30px;
        border-radius: 10px;
      }
    }
    .right {
      width: 100%;
      height: 250px;
      position: relative;
      .user {
        width: 100%;
        // height: 30px;
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #cccccc;
        padding-bottom: 10px;
        .username {
          font-size: 28px;
          font-weight: 800;
        }
        .logout {
          width: 80px;
          height: 26px;
          border-radius: 7px;
          font-size: 13px;
          color: #666666;
          border: 1px solid #cccccc;
          text-align: center;
          line-height: 26px;
          cursor: pointer;
        }
      }
      .userInfo {
        display: flex;
      }
      .grademain {
        width: 50px;
        height: 20px;
        // border: 1px solid #e03a24;
        box-shadow: 0 0 2px #e03a24;
        color: #e03a24;
        border-radius: 15px;
        font-size: 13px;
        text-align: center;
        line-height: 20px;
        font-style: italic;
        font-weight: bold;
      }

      .button {
        display: flex;
        width: 100%;
        height: 50px;
        margin-top: 20px;
      }
      .buttonItem {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 80px;
        height: 40px;
        cursor: pointer;
        border-left: 1px solid #cccccc;
        p {
          font-size: 20px;
          color: #666666;
        }
        .item {
          font-size: 12px;
          color: #666666;
        }
      }
      .buttonItem:nth-child(1) {
        border: none;
        margin-left: -20px;
      }
      .userId,
      .region {
        width: 100%;
        color: #666666;
        font-size: 13px;
        margin-top: 15px;
      }
    }
  }
}
</style>